//
//  MineTagsTableViewCell.swift
//  MT
//
//  Created by M on 2020/1/4.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class MineTagsTableViewCell: BaseMTInfoVerificationCell {
    
    var models: [MTTagModel]? {
        didSet {
            collectionView.models = models
            collectionView.reloadData()
        }
    }
    
    var colorStyle: MTTagCollectionView.ColorStyle = .background0x161616 {
        didSet {
            contentView.backgroundColor = colorStyle.backgrounColor
            collectionView.colorStyle = colorStyle
            collectionView.reloadData()
        }
    }

    lazy var collectionView: MTTagCollectionView = {
        let cv = MTTagCollectionView(frame: self.contentView.bounds)
        cv.isUserInteractionEnabled = false
        return cv
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension MineTagsTableViewCell {
    
    override func prepareForReuse() {
        super.prepareForReuse()
        titleLabel.textColor = UIColor(hex: 0x1d1a1a)
        titleLabel.font = PingFangSCSemiboldFont(14)
        underline.backgroundColor = UIColor(hex:0xe7e7e7)
    }
    
}

// MARK: ------------------------------------ 初始化 ----------------------------------------
extension MineTagsTableViewCell {
    
    private func initializeUI() {
        selectionStyle = .none
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.bottom.right.left.equalTo(contentView)
        }
        
        titleLabel.snp.remakeConstraints { (make) in
            make.top.equalTo(contentView).offset(20)
            make.left.equalTo(16)
        }
        
        accessoryImageIcon.snp.remakeConstraints { (make) in
            make.right.equalTo(contentView).offset(-16)
            make.centerY.equalTo(titleLabel)
            make.width.height.equalTo(14)
        }
        
        
        underline.snp.remakeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(1)
            make.height.equalTo(0.5)
        }
    }
    
}

// MARK: ------------------------------------ 计算 ----------------------------------------
extension MineTagsTableViewCell {
    
    public class func height(for models: [MTTagModel]) -> CGFloat {
        if models.count == 0 {
            return 0
        }
        let cvMaxWidth = ScreenWidth
        ///
        let rowHeight: CGFloat = 26.0 + 16.0
        /// initial height
        var cvHeight: CGFloat = 26.0 + 26.0 + 16.0
        
        var tw: CGFloat = 0
        for m in models {
            guard let name = m.name else { continue }
            let newWidth = name.width(font: PingFangSCMediumFont(12), maxHeight: 26) + 24 + 20
            if tw + newWidth > cvMaxWidth {
                cvHeight += rowHeight
                tw = newWidth
                continue
            }
            tw += newWidth
        }
        return cvHeight + (isIPhoneX ? 46 : 0)
    }
    
}
